<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="商家建议">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="商家名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.nickname"
                  placeholder="输入商家名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="商家账号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.username"
                  placeholder="输入商家账号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="提交时间">
                <a-range-picker  v-model:value="pickerDate" @change="handleChangeTime" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="businessColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'type'">
              <a-tag v-if="record.type === 1" color="green">功能异常</a-tag>
              <a-tag v-if="record.type === 2" color="orange">产品建议</a-tag>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="handleSee(record.id,2)" type="primary">查看</a-button>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <detailsModal ref="detailsModalRef" />
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { businessColumns } from "./columns.js";
import { queryFeedbackPage,exportFeedback } from "@/api/suggest"
import detailsModal from "./detailsModal.vue";
import { exporeFile } from "@/utils/exporeFile";
const total = ref(0);
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: total.value,
  showSizeChanger: true,
  pageSizeOptions: ["10", "20", "50", "100"],
  showTotal: () => `共 ${total.value} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  feedbackType: 2,
  username: undefined,
  nickName: undefined,
  startTime: undefined,
  endTime: undefined,
});
const dataSource = ref([]);
const queryFeedbackPageList = async () => {
  const data = await queryFeedbackPage(formState.value)
  if(data.code === 200) {
    dataSource.value = data.data.list;
    pagination.value.total = data.data.total;
  }
}
// 修改时间
const pickerDate = ref(null)
function handleChangeTime(date, dateString) {
  pickerDate.value = date
  formState.value.startTime = dateString[0] + ' 00:00:00';
  formState.value.endTime = dateString[1] + ' 23:59:59';
}
function handleSearch() {
  formState.value.page = 1
  formState.value.pageSize = 10
  queryFeedbackPageList();
}
function handleReset() {
  pickerDate.value = null
  formState.value = {
    page: 1,
    pageSize: 10,
    feedbackType: 2,
    username: undefined,
    nickName: undefined,
    startTime: undefined,
    endTime: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  queryFeedbackPageList()
}

const handleExport =async () => {
  const data = await exportFeedback(formState.value)
  exporeFile("商家建议列表.xls", data);
}
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  queryFeedbackPageList()
}
// 查看详情
const detailsModalRef = ref(null)
function handleSee(id,feedbackType) {
  detailsModalRef.value.open(id,feedbackType)
}
onMounted(() => {
  queryFeedbackPageList()
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
